<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<%
  String path = request.getContextPath();
%>
<jsp:include page="/pages/commons/top.jsp" />
<jsp:include page="/pages/commons/left.jsp" />

<div data-options="region:'center',title:'人员管理',iconCls:'icon-ok'"> 
    <div class="easyui-layout" data-options="fit:true">  
                <div data-options="region:'west',split:true,border:false" style="width:200px">
				   <table id="dept_dg" title="部门列表" class="easyui-treegrid" style="height: 477px" url="<%=path %>/dept/getTreeList.do?id=0" animate="true" treeField='deptName' idField="id" fitColumns="true" method="post" singleSelect="true">
				       <thead>
			           <tr>
					       <th field="deptName" width="30"></th>
					   </tr>
					   </thead>
				   </table>
				</div>  
                <div data-options="region:'center',border:false">
				    <table id="dg" title="人员列表" class="easyui-datagrid" style="width: 950px; height: 477px" toolbar="#toolbar"  fitColumns="true" method="post" singleSelect="false" loadMsg="数据载入中...">
		<thead>
			<tr>
			    <th data-options="field:'ck',checkbox:true"></th> 
			    <th field="id" width="30">流水号</th>
				<th field="userName" width="50">用户名称</th>
				<th field="realName" width="50">真实名称</th>
				<th field="tel" width="50">座机</th>
				<th field="mobile" width="50">手机</th>
				<th field="address" width="50">地址</th>
				<th field="email" width="50">Email</th>
				<th field="position" width="50">职位</th>
				<th field="userLevelName" width="50">用户级别</th>
				<th field="createTimeStr" width="50">创建日期</th>
			</tr>
		</thead>
	</table>
	<div id="toolbar" style="padding:5px;height:auto"> 
        <div style="margin-bottom:5px">  
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">添加人员</a> 
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">修改人员</a> 
		    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="delUser()">删除人员</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-redo" plain="true" onclick="reset()">重置密码</a>
		</div>
		<div> 
		    <label>用户名称：</label> <input id="queryUserNameObj" name="queryUserName" class="easyui-validatebox"/>
		    <label>用户级别：</label> <input id="queryUserLevelObj" name="queryUserLevel" url="<%=path%>/dictionary/getList.do?dictType=USER_LEVEL" class="easyui-combobox" valueField="dictValue" textField="dictName"/>
		    <label>真实名称：</label> <input id="queryRealNameObj" name="queryRealName" class="easyui-validatebox"/>
		    <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="queryUser()">查询</a>  
        </div>	
	</div>
	</div>  
    </div>   
    
	</div>
	

	<div id="dlg" class="easyui-dialog" style="width: 600px; height: 350px; padding: 10px 20px" closed="true" buttons="#dlg-buttons">
		<div class="ftitle">人员信息</div>
		<form id="fm" method="post" novalidate>
			<div class="fitem">
				<label>用户名称：</label> <input id="userNameObj" name="userName" class="easyui-validatebox" required="true" validType="length[0,50]"/>
				<label>用户级别：</label> <input id="userLevelObj" name="userLevel" class="easyui-combobox" required="true" url="<%=path%>/dictionary/getList.do?dictType=USER_LEVEL" valueField="dictValue" textField="dictName"/>
			</div>
			<div class="fitem">
				<label>真实名称：</label> <input id="realNameObj" name="realName" class="easyui-validatebox" required="true" validType="length[0,50]"/>
				<label>职位：</label> <input id="positionObj" name="position" class="easyui-validatebox" validType="length[0,50]"/>
			</div>

			<div class="fitem">
				<label>座机：</label> <input id="telObj" name="tel" class="easyui-validatebox"  validType="length[0,50]"/>
				<label>手机：</label> <input id="mobileObj" name="mobile" class="easyui-validatebox"  validType="length[0,50]"/>
			</div>
			<div class="fitem">
				<label>Email：</label> <input id="emailObj" name="email" class="easyui-validatebox"  validType="email"/>
			</div>
			
			<div class="fitem">
				<label>地址：</label> <input id="addressObj" name="address" style="width:350px" class="easyui-validatebox" validType="length[0,100]"/>
			</div>
            <input name="id" id="idObj" value="" type="hidden" />

		</form>
	</div>
	<div id="dlg-buttons">
		<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser()">保存</a> <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
	</div>
</div>

<jsp:include page="/pages/commons/footer.jsp" />


<script type="text/javascript">  
        var url;
        var deptId = 0;
        function newUser(){ 
		    var row = $('#dept_dg').datagrid('getSelected');
        	if(row == null){
        		$.messager.alert('错误','请先选择部门！','error'); 
        		return false;
        	}else{
			    deptId = row.id;
			}
            $('#dlg').dialog('open').dialog('setTitle','添加人员');  
            $('#fm').form('clear'); 
            url = "<%=path%>/user/add.do";  
        } 
		
        

        function editUser(){  
            var row = $('#dg').datagrid('getSelected');  
            if (row){  
                $('#dlg').dialog('open').dialog('setTitle','修改人员');  
                $('#fm').form('load',row);
                deptId = row.deptId; 
                url = "<%=path%>/user/mod.do";
            }  
        } 

        function reset(){  
            var row = $('#dg').datagrid('getSelected');  
            if (row){  
                url = "<%=path%>/user/reset.do?id="+row.id;
				$.post(url,null,saveCallBack);  
            }  
        } 		
		
		$('#dept_dg').treegrid({
		    onClickRow:function(row){
        		$('#dg').datagrid({url:"<%=path%>/user/getList.do?deptId="+row.id});
		    }
		});
        
       
        function saveUser(){ 
            var userName = $("#userNameObj").val();
            var userLevel = $("#userLevelObj").combobox('getValue');
            var realName = $("#realNameObj").val();
            var position = $("#positionObj").val();
            var tel = $("#telObj").val();
            var mobile = $("#mobileObj").val();
            var email = $("#emailObj").val();
            var address = $("#addressObj").val();
			if($('#fm').form('validate')){
			    if(!isNumberOr_Letter(userName)){
				    $.messager.alert('错误','用户名只能输入英文，数字或者下划线！','error'); 
				    return false;
				}
			    var data = "userName="+userName+"&deptId="+deptId;
			    data += "&userLevel="+userLevel;
			    data += "&realName="+realName;
			    data += "&position="+position;
			    data += "&tel="+tel;
			    data += "&mobile="+mobile;
			    data += "&email="+email;
			    data += "&address="+address;
			    if($("#id").val()!=''){
			    	data += "&id="+$("#idObj").val();
	            }
			    $.post(url,data,saveCallBack);  
			}
        } 

        function saveCallBack(result){
            var results = result.split("|"); 
	        if(results[0] == "error"){
	            $.messager.alert('错误',getErrorInfo(results[1]),'error'); 
	            return false;
            }else if(results[0] == "success"){
	            $.messager.alert('成功',getErrorInfo(results[1]),'info'); 
	            $('#dlg').dialog('close');      // close the dialog  
                $('#dg').datagrid('reload');
    	        return true;
            } 
        }	
 
        function delUser(){  
            var row = $('#dg').datagrid('getSelections');  
            if (row.length > 0){  
                $.messager.confirm('Confirm','确定删除该人员吗?',function(r){  
                    if (r){  
						var data = "";
						for(var i=0;i<row.length;i++){
						    data += row[i].id
						    if(i<row.length - 1){
						        data+=",";
							}
						}
                    	$.post("<%=path%>/user/del.do",{'ids':data},delCallBack);   
                    }  
                });  
            }  
        } 
        
        function delCallBack(result){
        	var results = result.split("|"); 
	        if(results[0] == "error"){
	            $.messager.alert('错误',getErrorInfo(results[1]),'error'); 
	            return false;
	        }else if(results[0] == "success"){
	            $.messager.alert('成功',getErrorInfo(results[1]),'info'); 
	            $('#dg').datagrid('reload');
    	        return true;
            } 
        } 
		
		function queryUser(){
		    var data = "?";
			data += "userName="+$("#queryUserNameObj").val();
			data += "&realName="+$("#queryRealNameObj").val();
			data += "&userLevel="+$("#queryUserLevelObj").combobox('getValue');
		    var queryUrl = "<%=path%>/user/getList.do"+data;
		    $("#dg").datagrid({url:queryUrl});
		}
       

    </script>
<style type="text/css">
#fm {
	margin: 0;
	padding: 10px 30px;
}

.ftitle {
	font-size: 14px;
	font-weight: bold;
	padding: 5px 0;
	margin-bottom: 10px;
	border-bottom: 1px solid #ccc;
}

.fitem {
	margin-bottom: 5px;
}

.fitem label {
	display: inline-block;
	width: 80px;
}
</style>

</html>
